package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.action.back.param.HotelQueryParam;
import com.qifan.alb2b.domain.HotelInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 酒店CURD的Dao
 * @author lj
 *
 */
@Repository
public class HotelInfoDao extends BaseDao{
	private static final Log logger=LogFactory.getLog(HotelInfoDao.class);
//	private int update;
	
	/**
	 * 根据条件分页查询酒店信息
	 * @param dgm 分页参数
	 * @param uqp 酒店查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,HotelQueryParam uqp){
		logger.debug("查询酒店");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from hotelinfo where 1=1");
		if(uqp.getHotelName()!=null&&!"".equals(uqp.getHotelName())){
			sql.append(" and hotelName like ?");
			params.add("%"+uqp.getHotelName()+"%");
		}
		if(uqp.getHotelLevel()>0){
			sql.append(" and hotelLevel=?");
			params.add(uqp.getHotelLevel());
		}
		if(uqp.getHotelAddress()!=null&&!"".equals(uqp.getHotelAddress())){
			sql.append(" and hotelAddr like ?");
			params.add("%"+uqp.getHotelName()+"%");
			
		}
		if(uqp.getCityId()!=null&&!"".equals(uqp.getCityId())){
			sql.append(" and cityId=?");
			params.add(uqp.getCityId());
		}
		if(uqp.getCityType()!=-1){
			sql.append(" and cityId in(select cityId from cityInfo where cityType=?)");
			params.add(uqp.getCityType());
		}
		//判断是否排序
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))
			sql.append(" order by ").append(dgm.getSort()).append(" ").append(dgm.getOrder());
//		return super.queryMapByPage(sql.toString(), dgm.getRows(), dgm.getPage(),params);
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, HotelInfoDomain.class);
	}
	
	public HotelInfoDomain getHotelById(String id){
		String sql="select * from hotelinfo where hotelId=?";
		List<HotelInfoDomain> list= JdbcUtil.queryForList(this, HotelInfoDomain.class, sql, id);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	

	public List<HotelInfoDomain> queryHotelsByCitys(String[] cityIds){
		StringBuilder sql=new StringBuilder();
		sql.append("select * from hotelInfo where cityId in(");
		for(int i=0;i<cityIds.length-1;i++){
			sql.append("?,");
		}
		sql.append("?)");
		return JdbcUtil.queryForList(this, HotelInfoDomain.class, sql.toString(), cityIds);
	}
	
	public List<HotelInfoDomain> queryHotelsByIds(String[] ids){
		StringBuilder sql=new StringBuilder();
		sql.append("select * from hotelInfo where hotelId in(");
		for(int i=0;i<ids.length-1;i++){
			sql.append("?,");
		}
		sql.append("?)");
		return JdbcUtil.queryForList(this, HotelInfoDomain.class, sql.toString(), ids);
	}
	
	/**
	 * 增加一个酒店信息
	 * @param HotelInfoDomain
	 */
	public void add(HotelInfoDomain hotelInfoDomain){
		super.addObj(hotelInfoDomain);
	}
	
	public void update(HotelInfoDomain hotelInfoDomain){
		super.updateObj(hotelInfoDomain, "hotelId");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 */
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from hotelinfo where hotelId in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
	
}
